<?php
require_once 'sr23_model.php';

$sr23 = new sr23_model();
if (!$_GET['n']) {
    print '<p>A Nutritional Database Number is Required.</p>';
    exit(); 
}
else
{
    // get the food long description
    $food = $sr23->get_long_description($_GET['n']);
}

/*
if (!$_GET['w']) {
    $weight = 100;
}
else 
{
    $weight = $_GET['weight'];
}
*/

$weight = $_GET['w'] ? $_GET['w'] : 100;

// calculate, use the $hundredgrams measurement as a test comparison when necessary
// $hundredgrams = $sr23->get_nutrient($_GET['n']);
$calculated = $sr23->calculate_nutrient_from_weight($_GET['n'], $weight);
// $sr23->print_pretty_array($hundredgrams);
// $sr23->print_pretty_array($calculated);

?>
<html>
  <head>
    <link rel='stylesheet' type='text/css' href='sr23.css' />  
  </head>
  <body>
    <form method='get'>
      <table>
        <tbody>
          <tr>
            <th>food:</th>
            <td>
              <?php echo $_GET['n'] . ': ' . $food; ?>
              <input name='n' type='hidden' value='<?php echo $_GET['n']; ?>'/>
            </td>
          </tr>  
          <tr>
            <th>weight in grams:</th>
            <td><input name='w' value='<?php echo $weight; ?>'/></td>
          </tr>
          <tr>
            <td colspan='2'>
              <input type='submit' value='calculate'/>
            </td>
          </tr>
        </tbody>
      </table>      
    </form>
    <p><a href='match_food.php'>choose different food type</a></p>

    <div id='results'>
      <table><tbody>
      <?php
          foreach ($calculated as $k => $v) {
              print "<tr><th>$k</th><td>$v</td></tr>";
          }
      ?>
      </tbody></table>
    </div>
  </body>
</html>  
